package com.example.myys.ui.base

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.itemsIndexed
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import coil3.compose.AsyncImage
import com.example.myys.data.entity.DataBean
import com.example.myys.ui.page.webPage.WebIndex

//带图弹窗
@SuppressLint("UnrememberedMutableState")
@Composable
@Preview
fun DialogWithImage(
    onDismissRequest: () -> Unit={},
    onPlay:()->Unit={},
    onConfirmation: () -> Unit={},
    dataBean:DataBean=DataBean(),
) {
    val context = LocalContext.current
    var isShowWeb by remember {
        mutableStateOf(false)
    }
    if (!isShowWeb) {
        Dialog(
            onDismissRequest = { onDismissRequest() },
            properties = DialogProperties(usePlatformDefaultWidth = false) // 禁用平台默认宽度限制
        ) {
            // Draw a rectangle shape with rounded corners inside the dialog
            Card(
                modifier = Modifier
                    .fillMaxWidth(0.9f)
                    .fillMaxHeight(0.6f)
                    .padding(7.dp),
            ) {
                Column {
                    Row(
                        modifier = Modifier
                            .height(200.dp)
                            .fillMaxWidth()
                            .padding(10.dp),
                    ) {
                        AsyncImage(
                            model = dataBean.cover,
                            contentDescription = dataBean.name,
                            modifier = Modifier
                                .width(150.dp)
                                .height(180.dp)
                                .padding(2.dp),
                            contentScale = ContentScale.Crop
                        )
                        Column {
                            Text(
                                modifier = Modifier.weight(1f),
                                text = "简介:${dataBean.desc}",
                                style = MaterialTheme.typography.bodySmall,
                                maxLines = 17,
                                overflow = TextOverflow.Ellipsis,
                                textAlign = TextAlign.Left
                            )
                            Row(
                                modifier = Modifier
                                    .height(40.dp)
                                    .fillMaxWidth(),
                                horizontalArrangement = Arrangement.spacedBy(4.dp),
                            ) {
                                Button(
                                    onClick = { onConfirmation() },
                                ) {
                                    Text("加入收藏")
                                }
                                Button(
                                    onClick = { onPlay() },
                                ) {
                                    Text("浏览")
                                }
                                Button(
                                    onClick = {
                                        isShowWeb = true
                                    },
                                ) {
                                    Text("web访问")
                                }
                                Button(
                                    onClick = { onDismissRequest() },
                                ) {
                                    Text("关闭")
                                }
                            }
                        }
                    }
                    Row(
                        modifier = Modifier
                            .fillMaxSize()
                            .padding(10.dp),
                    ) {
                        LazyVerticalGrid(
                            columns = GridCells.Adaptive(minSize = 100.dp),
                            horizontalArrangement = Arrangement.spacedBy(10.dp),
                            verticalArrangement = Arrangement.spacedBy(10.dp)
                        ) {
                            itemsIndexed(dataBean.dataSource) { index, item ->
                                Button(onClick = { onPlay() }) {
                                    Text(text = item.name + "(${item.episodes.size})")
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (isShowWeb) {
        Dialog(onDismissRequest = { isShowWeb = false }) {
            WebIndex(url = dataBean.url, onClose = { isShowWeb = false })
        }
    }
}